﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMSModel;

public partial class Topics_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            bd();
        }
    }

    private void bd()
    {
        CMSEntities cms = new CMSEntities();
        ddlType.DataSource = cms.TopicsType;
        ddlType.DataTextField = "Name";
        ddlType.DataValueField = "ID";
        ddlType.DataBind();
        var data=from a in cms.TopicsType
                 from b in cms.Topics
                 where a.ID==b.TypeId && b.TypeId==ddlType.SelectedValue
                 orderby b.CTime descending
                 select new {b.TypeId,a.Name,b.CTime,typename= a.Name,b.ID};    
        Repeater1.DataSource = data;
        Repeater1.DataBind();
        var id= cms.TopicsType.First(x => x.ID == ddlType.SelectedValue).NID;
        hidTopicsId.Value = id==null ?"":id.Value.ToString();
    }
    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        CMSEntities cms = new CMSEntities();
        var data = from a in cms.TopicsType
                   from b in cms.Topics
                   where a.ID == b.TypeId && b.TypeId == ddlType.SelectedValue
                   orderby b.CTime descending
                   select new { b.TypeId, a.Name, b.CTime, typename = a.Name, b.ID };
        Repeater1.DataSource = data;
        Repeater1.DataBind();
        var id = cms.TopicsType.First(x => x.ID == ddlType.SelectedValue).NID;
        hidTopicsId.Value = id == null ? "" : id.Value.ToString();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string id = Request["select"];
        if (!string.IsNullOrEmpty(id))
        {
            string[] socks = id.Split(',');
            if (socks.Length > 1)
            {
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('只能选择一项');", true);
                return;
            }
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(),"aa","alert('必须选择一项');",true);
        }
        CMSEntities cms = new CMSEntities();
        var type = cms.TopicsType.First(x => x.ID == ddlType.SelectedValue);
        type.NID = new Guid(id);
        cms.SaveChanges();
        ClientScript.RegisterStartupScript(GetType(), "aa", "alert('保存成功');document.location=document.location;", true);
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        var typeId = ((HiddenField)e.Item.FindControl("hidTypeId")).Value;
        var id = ((HiddenField)e.Item.FindControl("hidId")).Value;
        var c = (HyperLink)e.Item.FindControl("HyperLink1");
        c.NavigateUrl = System.Configuration.ConfigurationManager.AppSettings[typeId] + "_edit.aspx?typeId=" + typeId + "&id=" + id;
    }
    protected void btnAddType_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddType.aspx");
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        var typeId = ddlType.SelectedValue;
        Response.Redirect(System.Configuration.ConfigurationManager.AppSettings[typeId] + ".aspx?typeId=" + typeId);
    }
}